﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace AIthin.Controls
{
    public partial class SructViewControl : UserControl
    {
        // Globale Variablen 
        Device device = null; // Das Rendering-Device 
        static CustomVertex.PositionColored[] v = new CustomVertex.PositionColored[3];
        static float fAngle;
        VertexBuffer vertexBuffer;
        public SructViewControl()
        {
            InitializeComponent();
            
            //      this.Show(); 
            v[0].X = -1f; v[0].Y = -1f; v[0].Z = 0f;
            v[1].X = 1f; v[1].Y = -1f; v[1].Z = 0f;
            v[2].X = 0f; v[2].Y = 1f; v[2].Z = 0f;
            v[0].Color = System.Drawing.Color.DarkGoldenrod.ToArgb();
            v[1].Color = System.Drawing.Color.MediumOrchid.ToArgb();
            v[2].Color = System.Drawing.Color.Cornsilk.ToArgb();

            
        }
        public bool InitializeGraphics()
        {
            try
            {
                // Die Hardware initialisieren und ein paar Parameter setzen 
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed = true; 
                presentParams.SwapEffect = SwapEffect.Discard; //no swap buffer
            //    presentParams.EnableAutoDepthStencil = true; //with depth buffer
            //    presentParams.AutoDepthStencilFormat = DepthFormat.D16; //16 bit depth
                
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
                //set up the transformation of world coordinates into camera or view space
                device.Transform.View = Matrix.LookAtLH(
                new Vector3(0f, 0f, -4f), //eye point 4.0 in front of the canvas
                new Vector3(0f, 0f, 0f), //camera looks at point 0,0,0
                new Vector3(0f, 1f, 0f)); //world's up direction is the y-axis
                //set up the projection transformation using 4 parameters:
                //1.: field of view = 45 degrees; 2.: aspect ratio = heigth / width = 1 = square window;
                //3.: near clipping distance = 0; 4.: far clipping distance = 10;
                device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1f, 0f, 10f);
                //Turn off culling, so the user sees the front and back of the triangle
                device.RenderState.CullMode = Cull.None;
                //Turn off lighting, since the triangle provides its own colors
                device.RenderState.Lighting = false;
                //set up the property that fills the triangle with colors
                device.VertexFormat = CustomVertex.PositionColored.Format;
                if (vertexBuffer != null) vertexBuffer.Dispose();//free the old vertexBuffer if any
                //Create a new vertex buffer on the graphics card and connect it to the device.
                vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3,
                                                device, 0, CustomVertex.PositionColored.Format,
                                                Pool.Default);
                vertexBuffer.SetData(v, 0, LockFlags.None); //Copy the vertices from main memory to graphics card memory.
                device.SetStreamSource(0, vertexBuffer, 0); //Tell the device to use the vertexBuffer on the graphics card.
                return true;
            }
            catch (DirectXException)
            {
                return false;
            }
        }
        private void Render()
        {
            if (device == null)
                return;
           
                // Backbuffer löschen und gleichzeitig die Zeichenfläche blau einfärben 
                device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);

                //rotate with an angular velocity = 0.1
                fAngle += 0.1f;
                device.Transform.World = Matrix.RotationY(fAngle);

                // Anfang der Szene
                device.BeginScene();

                // An dieser Stelle würden Objekte gerendert werden 
                device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, v);
                //device.DrawUserPrimitives(PrimitiveType.TriangleList, 0, 1);
                // Ende der Szene 
                device.EndScene();
                device.Present();
           
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render();
        }
        protected void DisposeGraphics()
        {
        }
        private void SructViewControl_Load(object sender, EventArgs e)
        {
            if (this.Created)
            {
              if (!this.InitializeGraphics()) // Direct3D initialisieren 
                {
                    MessageBox.Show("Konnte Hardware nicht initialisieren.");

                }
            }
        }

        private void SructViewControl_MouseMove(object sender, MouseEventArgs e)
        {
            this.Render(); // Rendern 
        }
    }
}
